package demo.prototype;

/**
 * @author wangql
 * 浅拷贝（浅克隆）
 * 原型模式示例类
 */
public class User implements Cloneable{
    private String name;
    private Address address;


    @Override
    protected Object clone() throws CloneNotSupportedException {
        System.out.println("浅拷贝");
        return (User)super.clone();
    }

    public User(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", address=" + address +
                '}';
    }

    protected Object deepClone() throws CloneNotSupportedException {
        System.out.println("深拷贝");
        User clone = (User) super.clone();
        clone.setAddress((Address) this.address.clone());
        return clone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}
